<template>
  <div class="login_container">
    <div class="login_box">
      <!--      头像区域-->
      <div class="image_box">
        欢迎登录
      </div>
      <!--      输入框区域-->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form" hide-required-asterisk
               label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" clearable placeholder="请输入用户名"
                    prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" clearable placeholder="请输入密码" prefix-icon="el-icon-lock"
                    show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="login_btn_primary" round type="primary" @click="login">登录</el-button>
          <el-button class="login_btn_info" round type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {ElMessage} from "element-plus";
import {request} from "../network/axios";

export default {
  name: "Longin",
  data() {
    return {
      //这是loginForm里的数据
      loginForm: {
        username: '',
        password: ''
      },
      //验证表单信息,验证的属性必须是data里的值
      loginFormRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      console.log(this);
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(valid => {
        let that
        that = this
        if (!valid) return
        request({
          url: '/login',
          params: this.loginForm
        }).then(res => {
          if (res.data.meta.status !== 200) {
            ElMessage.error("登陆失败，请重新输入")
            console.log("登陆失败")
          } else {
            ElMessage.success("登陆成功")
            console.log(res.data.data.token)
            window.sessionStorage.setItem("token", res.data.data.token)
            this.$router.push('/home')
          }
        })
      })
    },
  }
}
</script>
<!--scoped 只在当前组件生效-->
<style scoped src="../assets/css/Login.css">
</style>